package com.miplus.ccrm.core.service.conf;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.core.controller.vo.LabelResVo;
import com.miplus.ccrm.core.controller.vo.conf.CmTrunkLinkEditVo;
import com.miplus.ccrm.core.controller.vo.conf.CmTrunkLinkReqVo;
import com.miplus.ccrm.core.controller.vo.conf.CmTrunkLinkResVo;
import com.miplus.ccrm.core.mapper.CmTrunkLinkMapper;
import com.miplus.ccrm.core.pojo.CmTrunk;
import com.miplus.ccrm.core.pojo.CmTrunkLink;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author wangzl
 * @description 针对表【cm_trunk】的数据库操作Service实现
 * @createDate 2025-07-12 14:26:17
 */
@Service
public class CmTrunkLinkServiceImpl extends MPJBaseServiceImpl<CmTrunkLinkMapper, CmTrunkLink>
        implements CmTrunkLinkService{

    @Override
    public IPage<CmTrunkLinkResVo> selectCmTrunkLinkList(CmTrunkLinkReqVo paras)
    {
        return selectJoinListPage(new Page<>(paras.getPageNum(),paras.getPageSize()),
                CmTrunkLinkResVo.class,
                new MPJLambdaWrapper<CmTrunkLink>()
                        .innerJoin(CmTrunk.class,"ct", CmTrunk::getTrunkid,CmTrunkLink::getTrunkid)
                        .in(ObjectUtils.isNotNull(paras.getCmtrunklist()),CmTrunkLink::getTrunkid,paras.getCmtrunklist())
                        .in(ObjectUtils.isNotNull(paras.getDeptlist()),CmTrunkLink::getDept,paras.getDeptlist())
                        .in(ObjectUtils.isNotNull(paras.getRolelist()),CmTrunkLink::getRoleid,paras.getRolelist())
                        .selectAll(CmTrunkLink.class)
                        .selectAs(CmTrunk::getName,CmTrunkLinkResVo::getName)
                        .selectAs(CmTrunk::getSymbolphone,CmTrunkLinkResVo::getSymbolphone)
                        .selectAs(CmTrunk::getStatus,CmTrunkLinkResVo::getStatus)
                        .selectAs("fun_getusername(t.crusr)",CmTrunkLinkResVo::getCrusrname)
                        .selectAs("fun_getusername(t.mdusr)",CmTrunkLinkResVo::getMdusrname)
                        .selectAs("(select grpname from sys_grp where grpid = t.dept)",CmTrunkLinkResVo::getDeptname)
                        .selectAs("(select dsc from sys_role where roleid = t.roleid)",CmTrunkLinkResVo::getRolename)
                        .orderByDesc(CmTrunkLink::getCrdt)
        );
    }
    @Override
    public CmdResVo addCmTrunkLink(String usrid, CmTrunkLinkEditVo paras)
    {
        if(paras.getDeptlist() == null || paras.getDeptlist().size() == 0)
            return new CmdResVo(-1,"部门不能为空");
        if(paras.getRolelist() == null || paras.getRolelist().size() == 0)
            return new CmdResVo(-1,"角色不能为空");
        if(paras.getCmtrunklist() == null || paras.getCmtrunklist().size() == 0)
            return new CmdResVo(-1,"线路不能为空");
        List<CmTrunkLink> cmTrunkLinks = new ArrayList<>();
        int ret = 0;
        for( String dept : paras.getDeptlist()) {
            for (String roleid : paras.getRolelist()) {
                for (Integer trunkid : paras.getCmtrunklist()) {
                    CmTrunkLink cmTrunkLink = getCurrentTrunkLink(dept, roleid, trunkid);
                    if (cmTrunkLink != null) {
                        cmTrunkLink.setMddt(new Date());
                        cmTrunkLink.setMdusr(usrid);
                        cmTrunkLink.setDsc(paras.getDsc());
                        updateById(cmTrunkLink);
                    } else {
                        CmTrunkLink tl = new CmTrunkLink();
                        tl.setTrunkid(trunkid);
                        tl.setDept(dept);
                        tl.setRoleid(roleid);
                        tl.setDsc(paras.getDsc());
                        tl.setCrdt(new Date());
                        tl.setCrusr(usrid);
                        cmTrunkLinks.add(tl);
                        ret++;
                    }
                }
            }
        }

        if(cmTrunkLinks.size() > 0)
            saveBatch(cmTrunkLinks);

        return new CmdResVo(ret,"增加成功。");
    }
    @Override
    public CmdResVo updateCmTrunkLink(String usrid, CmTrunkLink paras)
    {
        if(paras == null || ObjectUtils.isNull(paras.getId()))
            return new CmdResVo(-1,"参数错误");
        CmTrunkLink cl = getById(paras.getId());
        if( cl == null)
            return new CmdResVo(-1,"未找到该配置");
//        cl.setDept(paras.getDept());
//        cl.setRoleid(paras.getRoleid());
        cl.setDsc(paras.getDsc());
        cl.setMdusr(usrid);
        cl.setMddt(new Date());
        updateById(cl);
        return new CmdResVo(0,"更新成功.");
    }
    @Override
    public CmdResVo deleteCmTrunkLink(Integer id)
    {
        removeById(id);
        return new CmdResVo(0,"删除成功.");
    }
    @Override
    public List<LabelResVo> getCmTrunkLinkList(String usrid)
    {
       return  baseMapper.getCmTrunkLinkList(usrid);
    }


    private CmTrunkLink getCurrentTrunkLink(String dept, String roleid, Integer cmTrunkId)
    {
        return baseMapper.selectOne( new LambdaQueryWrapper<CmTrunkLink>()
                .eq(CmTrunkLink::getDept,dept)
                .eq(CmTrunkLink::getRoleid, roleid)
                .eq(CmTrunkLink::getTrunkid,cmTrunkId)
                .last("limit 1")
        );
    }

}




